//
//  StaffConfirmViewController.m
//  NtoccShipper
//
//  Created by 张天兴 on 2024/11/11.
//  Copyright © 2024 zhuchongjin. All rights reserved.
//

#import "StaffConfirmViewController.h"
#import "StaffCodingTextFieldCell.h"
#import "StaffConfirmTextFieldCell.h"
#import "ZTXCarTeamController.h"
#import "ZTXSelectedTeamViewController.h"
#import "AddOtherFeeVC.h"
#import "CarTeamModel.h"

@interface StaffConfirmViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong) CJTableView *tableView;
@property (nonatomic, strong) CJButton *contentButton;
@property (nonatomic, strong) NSArray *priceArr;
@property (nonatomic, strong) NSArray *selectedArr;

@property (nonatomic, strong) CJTextField *HZPriceTextField;
@property (nonatomic, strong) CJTextField *CJPriceTextField;
@property (nonatomic, strong) CJTextField *YCPriceTextField;
@property (nonatomic, strong) NSString *settlementType;
@property (nonatomic, strong) NSString *erase;
@property (nonatomic, strong) NSString *settlementTypeStr;
@property (nonatomic, strong) NSString *eraseStr;
@property (nonatomic, strong) UILabel *jsLabel;
@property (nonatomic, strong) UILabel *mlLabel;
@property (nonatomic, strong) UILabel *dsLabel;
@property (nonatomic, strong) UILabel *qtLabel;
@property (nonatomic, strong) NSString *otherStr;
@property (nonatomic, strong) NSString *payerReceivercode;
@property (nonatomic, strong) NSString *payerReceiverStr;
@property (nonatomic, strong) NSString *orderExtraFeeListStr;
@property (nonatomic, strong) NSMutableArray *otherList;
@end

@implementation StaffConfirmViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    [self setTitleName:CJLString(@"确认核算")];
    [self setBackLeftButton:@""];
    self.priceArr = @[@"货主成交单价(元)",@"司机承运单价(元)",@"异常费用"];
    self.selectedArr = @[@"结算规则",@"抹零规则",@"代收款人",@"其他费用"];
    self.tableView.placeHolderView = [[KKTableViewNoDataView alloc] initWithFrame:self.tableView.bounds image:[UIImage imageNamed:@"no_data"] viewClick:^{
    }];
    self.HZPriceTextField.maxLength = 10;
    self.CJPriceTextField.maxLength = 10;
    self.YCPriceTextField.maxLength = 10;
    self.settlementType =@"";
    self.erase =@"";
    self.otherList = [@[] mutableCopy];
    [self.tableView registerNib:[UINib nibWithNibName:@"StaffCodingTextFieldCell" bundle:nil] forCellReuseIdentifier:@"StaffCodingTextFieldCell"];
    [self.tableView registerNib:[UINib nibWithNibName:@"StaffConfirmTextFieldCell" bundle:nil] forCellReuseIdentifier:@"StaffConfirmTextFieldCell"];

    [self.view addSubview:self.tableView];
    [self.view addSubview:self.contentButton];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return self.priceArr.count;
    }else{
        return self.selectedArr.count;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        StaffConfirmTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"StaffConfirmTextFieldCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = self.priceArr[indexPath.row];
        if (indexPath.row == 0) {
            cell.textField.delegate = self.HZPriceTextField.delegate = self;
            cell.textField.textFieldType = CJTextFieldStringTypeMoneyValue;
            cell.textField.keyboardType = UIKeyboardTypeDecimalPad;
            cell.textField.maxLength = 10;
            cell.textField.placeholder = @"填写需修改的货主成交单价";
            self.HZPriceTextField = cell.textField;
        }else if (indexPath.row == 1){
            cell.textField.delegate = self.CJPriceTextField.delegate = self;
            cell.textField.textFieldType = CJTextFieldStringTypeMoneyValue;
            cell.textField.keyboardType = UIKeyboardTypeDecimalPad;
            cell.textField.maxLength = 10;
            cell.textField.placeholder = @"填写需修改的司机承运单价";
            self.CJPriceTextField = cell.textField;
          
        }else{
            cell.textField.delegate = self.CJPriceTextField.delegate = self;
            cell.textField.textFieldType = CJTextFieldStringTypeMoneyValue;
            cell.textField.keyboardType = UIKeyboardTypeDecimalPad;
            cell.textField.maxLength = 10;
            cell.textField.placeholder = @"填写需要的异常费用";
            self.YCPriceTextField = cell.textField;
        }
        return cell;
    }else{
        StaffCodingTextFieldCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"StaffCodingTextFieldCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.nameLabel.text = self.selectedArr[indexPath.row];
        if (indexPath.row == 0) {
            self.jsLabel = cell.cententLabel;
        }else if (indexPath.row == 1){
            self.mlLabel = cell.cententLabel;
        }else if (indexPath.row == 2){
            self.dsLabel = cell.cententLabel;
        }else{
            self.qtLabel = cell.cententLabel;
        }
        return cell;
    }
        
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1 && indexPath.row == 0) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"请选择" message:@"结算类型" preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *updateAct = [UIAlertAction actionWithTitle:@"按装货数量" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.settlementType = @"0";
            self.jsLabel.text = self.settlementTypeStr = @"按装货数量";
        }];
        UIAlertAction *twodateAct = [UIAlertAction actionWithTitle:@"按卸货数量" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.settlementType = @"1";
            self.jsLabel.text = self.settlementTypeStr = @"按卸货数量";
        }];
        UIAlertAction *theedateAct = [UIAlertAction actionWithTitle:@"按装卸货数量最小值" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.settlementType = @"2";
            self.jsLabel.text = self.settlementTypeStr = @"按装卸货数量最小值";
        }];
        UIAlertAction *cententAct = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertC addAction:updateAct];
        [alertC addAction:twodateAct];
        [alertC addAction:theedateAct];
        [alertC addAction:cententAct];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertC animated:YES completion:^{
            
        }];
    }else if (indexPath.section == 1 && indexPath.row == 1){
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction *change = [UIAlertAction actionWithTitle:@"不抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.erase = @"0";
            self.mlLabel.text = self.eraseStr = @"不抹零";
        }];
        UIAlertAction *seeBig = [UIAlertAction actionWithTitle:@"角分抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.erase = @"1";
            self.mlLabel.text = self.eraseStr = @"角分抹零";
        }];
        UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"五元下抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.erase = @"2";
            self.mlLabel.text = self.eraseStr = @"五元以下抹零";
        }];
        UIAlertAction *cancleTen = [UIAlertAction actionWithTitle:@"十元下抹零" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.erase = @"3";
            self.mlLabel.text = self.eraseStr = @"十元以下抹零";
        }];
        UIAlertAction *centent = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertVC addAction:change];
        [alertVC addAction:seeBig];
        [alertVC addAction:cancle];
        [alertVC addAction:cancleTen];
        [alertVC addAction:centent];
        [self presentViewController:alertVC animated:YES completion:nil];
    }else if(indexPath.section == 1 && indexPath.row == 2){
        ZTXSelectedTeamViewController *VC = [[ZTXSelectedTeamViewController alloc] init];
        VC.strCode = self.orderDetailCodes;
        VC.blockSeletModel = ^(id anyObject) {
            CarTeamModel *model = anyObject;
            self.dsLabel.text =  self.payerReceiverStr = model.name;
            self.payerReceivercode = model.partyCode;
        };
        [self pushVc:VC];
    }else if (indexPath.section == 1 && indexPath.row == 3){
        AddOtherFeeVC *vc = [[AddOtherFeeVC alloc] init];
        vc.arrList = self.otherList;
        WS(ws);
        vc.blockSuccess = ^(NSString *str, NSMutableArray *list) {
            self.otherList = list;
            ws.otherStr = [CJControl convertToJsonDataFromArray:(NSArray *)list];
            ws.qtLabel.text = str;
            NSLog(@"---%@",ws.otherStr);
        };
        [self pushVc:vc];
    }
}
- (CJTableView *)tableView{
    if (!_tableView) {
        _tableView = CJTableView.tbInitFrameStyle(CGRectMake(0, CJTableToTopHeight + kTopHeight, SCREEN_WIDTH, SCREEN_HEIGHT-kTopHeight - kNavBarHeight -CJTableToTopHeight - 30),UITableViewStylePlain)
        .tbDelegate(self)
        .tbDataSource(self)
        .tbSetOther(YES)
        .tbEstimatedRowHeight(YES)
        .tbMjHeadFooter(NO)
        ;
    }
    return _tableView;
}
- (CJButton *)contentButton{
    if (!_contentButton) {
        _contentButton = CJButton.btnInit
        .btnFrame(CGRectMake(50,CGRectGetMaxY(self.tableView.frame) + 10,SCREEN_WIDTH - 100,50))
        .btnText(@"确认核算")
        .btnNormalTitleColor([UIColor cjWhiteColor])
        .btnBgColor([UIColor cjMainColor])
        .btnTargetAction(self, @selector(btnCancleAction:))
        .btnCornerRadius(10)
        .btnCornerWidth(1)
        .btnCornerColor([UIColor cjMainColor])
        ;
    }
    return _contentButton;
}
- (void)btnCancleAction:(UIButton *)btn{
    NSLog(@"%@---%@-----%@",self.HZPriceTextField.text,self.CJPriceTextField.text,self.YCPriceTextField.text);
    NSString *strURL = CJStringWithFormat(@"%@/ntocc-tms-api/orderDetail/saveBatchCalculated",CJBaseUrl);
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:self.HZPriceTextField.text forKey:@"taxPrice"];
    [dic setValue:self.CJPriceTextField.text forKey:@"realPrice"];
    [dic setValue:self.settlementType forKey:@"tonnage"];
    [dic setValue:self.erase forKey:@"erase"];
    [dic setValue:@"true" forKey:@"isSubmit"];
    [dic setValue:self.YCPriceTextField.text forKey:@"abnormalFee"];
    [dic setValue:self.qtLabel.text forKey:@"extraFee"];//其他费用
    [dic setValue:self.otherStr forKey:@"orderExtraFeeListStr"];
    [dic setValue:self.payerReceivercode forKey:@"payerReceiver"];//代收人
    [dic setValue:self.orderDetailCodes forKey:@"orderDetailCode"];
    WS(ws);
    [[HDNetworking sharedHDNetworking] jsonTaskWithHTTPMethod:PostType urlString:strURL withParameter:dic status:OperationTip success:^(id  _Nonnull responseObject) {
        [ws pop];
        } failure:^(NSError * _Nonnull error) {

        }];

}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
